package com.hlingsoft.cms.action;

import com.hlingsoft.cms.dao.FeedbackDao;
import com.hlingsoft.cms.entity.Feedback;
import com.hlingsoft.cms.util.Common;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created by selbylei on 2015/5/25.
 */
@Controller
@RequestMapping("/feedback")
public class FeedbackAction extends BaseAction {

    @Autowired
    private FeedbackDao feedbackDao;


    /**
     * 提出建议
     * @param appUserId
     * @param content
     * @param email
     * @param request
     * @return
     */
    @RequestMapping(value = "/add.json")
    @ResponseBody
    public Map<String, Object> add(
            @RequestParam(value = "appUserId",required = false) Integer appUserId,
            @RequestParam(value = "content",required = false) String content,
            @RequestParam(value = "email",required = false) String email,
            HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            Feedback feedback = new Feedback();

            if (appUserId!=null){
                feedback.setAppUserId(appUserId.longValue());
            }
            if (content!=null){
                feedback.setContent(Common.encodeStr(content));
            }
            if (email!=null){
                feedback.setEmail(Common.encodeStr(email));
            }
            Date now = new Date();
            feedback.setCreateTime(now);
            feedback.setUpdateTime(now);

            feedbackDao.addFeedback(feedback);
            resultMap.put("result",true);
        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", false);
        }
        return resultMap;
    }
}
